/**
* 创建者：luyiming
* 该组件用于：
* 创建时间： 2019-12-24 17:55:43
* @param 参数说明
* @emit 事件使用说明
*/

import { rcimTree } from '@/api/RCIM/RCIM-system.js';
import ScrollPane from '@/components/luyiming/ScrollPane/ScrollPane.vue'
export default {
	name: 'SystemTree_cmp',
	components: {
		ScrollPane
	},
	props: {
		// 这是示例数据的通途
		example: {
			type: Array,
			default: () => []
		},
		nodeType: {
			type: Array,
			default: () => ["root"]
		},
		// 控制展示几棵树，默认是区域和组织架构树
		showTreeType: {
			type: Array,
			default: () => ['area', 'org']
		},
		// 控制展示到哪个子节点
		leaf: {
			type: String,
			default: () => 'all'
		},
	},
	// 数据
	data() {
		return {
			filterText: '',
			// 树的类型
			treeType: 'area',
			treeData: {},
			data: [],
			treeProps: {
				label: 'name',
			},
			// 初始化被选中的节点
			currentNodeKey: "",
			// 初始化默认展开的树节点数组
			defaultExpand: [],
			loading: false,
		}
	},
	// 监听
	watch: {
		filterText(val) {
			this.$refs.tree.filter(val);
		},
		treeType(val) {
			this.data = this.treeData[val]
		},
		data(){
			this.$nextTick(() => {
				this.loading = false;
				const nodeDef = this.getDefaultChooseNode(this.treeType)
				// this.currentNodeKey = nodeDef[0].id
				this.$refs.tree.setCurrentKey(nodeDef[0].id)
				this.defaultExpand = [nodeDef[0].id]
				const node = this.$refs.tree.getCurrentNode()
				const Node = this.$refs.tree.getNode(node)
				const tree = this.$refs.tree
				this.treeHandle(node, Node, tree)
			})
		},
	},
	// Vue实例初始化完成
	created() {
		this.loading = true;
		this.treeType = this.showTreeType[0];
		Promise.all([
			rcimTree({
				type: 'org'
			}),
			rcimTree({
				type: 'area'
			})
		]).then(res => {
			console.log(res);
			let tree = null
			if(Object.prototype.toString.call(res[0].data).slice(8,-1) === 'Object') {
				tree = {
					org: this.getUnLeafTree(this.leaf,[res[0]?.data || {}]),
					area: this.getUnLeafTree(this.leaf,[res[1]?.data || {}])
				}
			} else {
				tree = {
					org: this.getUnLeafTree(this.leaf,res[0]?.data || []),
					area: this.getUnLeafTree(this.leaf,res[1]?.data || [])
				}
			}
			this.treeData = tree
			this.data = this.treeData[this.treeType]

		})
	},
	// Vue挂载完成
	mounted() {
		
	},
	// Vue实例销毁前
	beforeDestroy() {

	},
	// 挂载的方法
	methods: {
        /**
        * 方法描述
        */
		getUnLeafTree(leaf, tree = []){
			if(leaf === 'all'){
				return tree;
			}
			return this.getTreeByType(leaf, tree);
		},
		getTreeByType(type, tree, result = []){
			tree.forEach(item => {
				// 如果类型不满足要求
				if(!~type.indexOf(item.type)){
					return
				}
				const Obj = Object.assign({}, 
					item, 
					{
						children: []
					}
					)
				result.push(Obj)
				if(item.children && item.children.length > 0){
					return	this.getTreeByType(type, item.children, Obj.children)
				}
				delete Obj.children;
			})
			return result;
		},
		getDefaultChooseNode(type="org"){
			const firstChoose = this.nodeType[0]
			const tree = this.treeData[type]
			const node = this.getChooseNode(tree, firstChoose)
			return node
			// 不是用源设备
			// if(param.type !== 'raddevice'){
			// 	return
			// }
		},
		getChooseNode(list = [], type = "root", result = []){
			list.forEach( item => {
				if(item.type === type){
					return result.push(item)
				}
				if(item.children){
					this.getChooseNode(item.children, type, result)
				}
			})
			return result
		},
		/**
		 * 根据Input框输入值筛选树
		 * @param { String } value
		 * @param { Object } data
		 */
		filterNode(value, data) {
			if (!value) return true;
			let isFilter = data.name.indexOf(value) !== -1
			if( isFilter ){
				this.setChildrenIsOk(data.children || [], value)
			}
			let isChildren = data.isValue === value
			return isFilter || isChildren;
		},
		/**
		 * 切换树的类型
		 * @param { String } value org | area
		 */
		treeTypeChange(value = 'org') {
			this.treeType = value;
		},
		/**
		 * 树的点击
		 */
		treeHandle(...rest){
			this.$emit('handleEvent', ...rest, this.treeType)
		},
		/**
		 * 筛选的时候，子集要ok的
		 */
		setChildrenIsOk(list, str){
			if(!(list instanceof Array) || list.length === 0){
				return
			}
			list.forEach( item => {
				item.isValue = str;
				item.children && this.setChildrenIsOk(item.children, str)
			})
		}
	},
}
